<template>
    <div>
       <div style="display: flex;justify-content: space-between">
           <el-button icon="el-icon-plus" type="primary" @click="showDialog">添加工资账套</el-button>
           <el-button icon="el-icon-refresh" type="success" @click="initSalary"></el-button>
           <el-dialog
                   :title="dialogTitle"
                   :visible.sync="dialogVisible"
                   :before-close="handleClose"
                   width="50%">
               <div style="display: flex;justify-content: space-around;align-items: center">
                   <el-steps direction="vertical" :active="activeIndex">
                       <el-step :title="salaryItemName" v-for="(salaryItemName,index) in salaryItemNames" :key="index"></el-step>
                   </el-steps>
                           <el-input :placeholder="'请输入' + salaryItemNames[index]" v-for="(value,key,index) in salary" :key="index" v-show="activeIndex  == index"
                                      style="width: 200px" v-model="salary[key]" @blur="check($event)" ref='input' v-focus>
                           </el-input>
               </div>
               <span slot="footer" class="dialog-footer">
    <el-button @click="preStep" v-show="activeIndex != 0">{{activeIndex == 10 ?'取消' :'上一步'}}</el-button>
    <el-button type="primary" @click="nextStep(activeIndex)">{{activeIndex == 10 ?'确定' :'下一步'}}</el-button>
  </span>
           </el-dialog>
       </div>
        <div style="margin-top: 10px">
            <el-table :data="salaries" border stripe  @selection-change="handleSelectionChange"  v-loading="loading"
                      element-loading-text="加载中">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column width="200" prop="name" label="账套名称"></el-table-column>
                <el-table-column width="70" prop="basicSalary" label="基本工资"></el-table-column>
                <el-table-column width="70" prop="trafficSalary" label="交通补助"></el-table-column>
                <el-table-column width="70" prop="lunchSalary" label="午餐补助"></el-table-column>
                <el-table-column width="70" prop="bonus" label="奖金"></el-table-column>
                <el-table-column width="100" prop="createDate" label="启用时间"></el-table-column>
                <el-table-column label="养老金" align="center">
                    <el-table-column width="70" prop="pensionPer" label="比率"></el-table-column>
                    <el-table-column width="70" prop="pensionBase" label="基数"></el-table-column>
                </el-table-column>
                <el-table-column label="医疗保险" align="center">
                    <el-table-column width="70" prop="medicalPer" label="比率"></el-table-column>
                    <el-table-column width="70" prop="medicalBase" label="基数"></el-table-column>
                </el-table-column>
                <el-table-column label="公积金" align="center">
                    <el-table-column width="70" prop="accumulationFundPer" label="比率"></el-table-column>
                    <el-table-column width="70" prop="accumulationFundBase" label="基数"></el-table-column>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button @click="showEditSalaryView(scope.row)">编辑</el-button>
                        <el-button type="danger" @click="deleteSalary(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
           <div style="display: flex;justify-content: space-between;margin-top: 10px">
               <el-button type="danger" @click="delIds" :disabled="multipleSelection.length == 0">批量删除</el-button>
               <el-pagination
                       background
                       layout="sizes, prev, pager, next, jumper, ->, total, slot"
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       :total="total">
               </el-pagination>
           </div>
        </div>

    </div>
</template>

<script>
    export default {
        name: "SalSob",
        data(){
            return {
                loading:false,
                dialogTitle:'添加工资账套',
                total:0,
                pageSize:10,
                current:0,
                dialogVisible:false,
                salaries:[],
                activeIndex:0,
                salaryItemNames: [
                    '基本工资',
                    '交通补助',
                    '午餐补助',
                    '奖金',
                    '养老金比率',
                    '养老金基数',
                    '医疗保险比率',
                    '医疗保险基数',
                    '公积金比率',
                    '公积金基数',
                    '账套名称'
                ],
                salary: {
                    basicSalary: 1,
                    trafficSalary: 0,
                    lunchSalary: 0,
                    bonus: 0,
                    pensionPer: 0,
                    pensionBase: 0,
                    medicalPer: 0,
                    medicalBase: 0,
                    accumulationFundPer: 0,
                    accumulationFundBase: 0,
                    name: ''
                },
                isNumber:true,
                count : 0,
                multipleSelection:[]
            }
        },
        mounted(){
            this.initSalary();
        },
        methods:{
            //修改
            showEditSalaryView(data){
                this.dialogVisible =true;
                this.dialogTitle ='修改工资账套';
                this.salary.basicSalary = data.basicSalary;
                this.salary.trafficSalary = data.trafficSalary;
                this.salary.lunchSalary = data.lunchSalary;
                this.salary.bonus = data.bonus;
                this.salary.pensionPer = data.pensionPer;
                this.salary.pensionBase = data.pensionBase;
                this.salary.medicalPer = data.medicalPer;
                this.salary.medicalBase = data.medicalBase;
                this.salary.accumulationFundPer = data.accumulationFundPer;
                this.salary.accumulationFundBase = data.accumulationFundBase;
                this.salary.name = data.name;
                this.salary.id = data.id;
            },
            handleSelectionChange(val){
                this.multipleSelection =val;
            },
            //批量删除
            delIds(){
                let ids = this.multipleSelection.map(item=>item.id).join();
                this.$confirm('此操作将永久删除【'+ this.multipleSelection.length+'】,条记录 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    this.deleteRequest('/api/salary/sob/ids/?ids=' + ids).then(resp=>{
                        if(resp){
                            this.initSalary();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'success',
                        message: '已取消删除'
                    });
                });
            },
            //删除
            deleteSalary(data){
                this.$confirm('此操作将永久删除【'+ data.name+'】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                   this.deleteRequest('/api/salary/sob/' + data.id).then(resp=>{
                       if(resp){
                           this.initSalary();
                       }
                   })
                }).catch(() => {
                    this.$message({
                        type: 'success',
                        message: '已取消删除'
                    });
                });
            },

            sizeChange(pageSize){
                this.pageSize = pageSize;
                this.initSalary();
            },
            currentChange(currentPage ){
                this.current = currentPage;
                this.initSalary();

            },
            //检查文本框输入内容
            check(e){
                let num= e.target.value;
                console.log(num+'num')
                // let keynum = window.event ? e.keyCode : e.which;
                // console.log(keynum)
                // let keychar = String.fromCharCode(keynum);
                // if(keynum ==189 || keynum == 190 || keynum == 110 || keynum == 109 || keynum<48 || keynum >57){
                //     this.$message.error("")
                //     e.target.value = ''
                // }
                // if(this.activeIndex ==10){
                //     if(!num){
                //         this.$message({
                //             type: 'error',
                //             message: '不能为空 ',
                //             duration: 1000,
                //             showClose: true,
                //         })
                //         this.isNumber = false;
                //         this.count =1
                //     }
                //     return;
                // }
               if(this.activeIndex !=10){
                   let regx = /^\d+\.?\d*$/;
                   const newNum_two = regx.test(num)
                   console.log(newNum_two + "===============")
                   if (!newNum_two) {
                       this.$message({
                           type: 'error',
                           message: '请输入正整数或者小数 ',
                           duration: 1000,
                           showClose: true,
                       })
                       this.isNumber = false;
                       this.count =1
                       return
                   }else{
                       this.isNumber = true;
                   }
               }
            },
            //点击x触发
            handleClose(){
                this.dialogVisible =false;
                this.activeIndex = 0;
                this.salary={
                    basicSalary: 0,
                    trafficSalary: 0,
                    lunchSalary: 0,
                    bonus: 0,
                    pensionPer: 0,
                    pensionBase: 0,
                    medicalPer: 0,
                    medicalBase: 0,
                    accumulationFundPer: 0,
                    accumulationFundBase: 0,
                    name: ''
                }
            },
            //上一步
            preStep(){

                if(this.activeIndex == 10){
                    this.dialogVisible =false;
                    return;
                }
                if(this.isNumber){
                    this.activeIndex--;
                }else{
                    if(this.count !=1){
                        this.$message({
                            type: 'error',
                            message: '请输入正整数或者小数 ',
                            duration: 1000,
                            showClose: true,
                        })
                    }else{
                        this.count =0
                    }
                }

            },
            //下一步
            nextStep(index){
                console.log(index + "-==")

                if(this.isNumber){
                    this.activeIndex++;
                    let _this = this
                    //聚焦
                    setTimeout(function () {
                        let inputFocus =_this.$refs.input[_this.activeIndex];
                        inputFocus.focus();
                    }, 1)
                }else{
                    if(this.count !=1){
                        console.log('请输入正整数或者小数' + this.count)
                        this.$message({
                            type: 'error',
                            message: '请输入正整数或者小数 ',
                            duration: 1000,
                            showClose: true,
                        })
                    }else{
                        this.count =0
                    }
                }
                if(this.activeIndex ==10){
                    if(!this.salary.name){
                        this.$message({
                            type: 'error',
                            message: '不能为空 ',
                            duration: 1000,
                            showClose: true,
                        })
                        return
                    }
                    if(this.salary.id){
                        this.putRequest('/api/salary/sob/',this.salary).then(resp=>{
                            if(resp){
                                this.dialogVisible =false;
                                this.initSalary();
                            }
                        })
                    }else{
                        this.postRequest('/api/salary/sob/',this.salary).then(resp=>{
                            if(resp){
                                this.dialogVisible =false;
                                this.initSalary();
                                this.activeIndex = 0;
                                this.salary={
                                    basicSalary: 0,
                                    trafficSalary: 0,
                                    lunchSalary: 0,
                                    bonus: 0,
                                    pensionPer: 0,
                                    pensionBase: 0,
                                    medicalPer: 0,
                                    medicalBase: 0,
                                    accumulationFundPer: 0,
                                    accumulationFundBase: 0,
                                    name: ''
                                }
                            }
                        })
                    }
                    return;
                }


            },
            //初始化数据
            initSalary(){
                this.loading =true
                this.getRequest('/api/salary/sob/?pageSize=' + this.pageSize + "&current=" + this.current).then(resp=>{
                    if(resp){
                        this.loading =false
                        this.salaries = resp.list;
                        this.total = resp.total;
                    }

                })
            },
            //点击添加工资套账
            showDialog(){
                this.dialogVisible =true;
                this.dialogTitle ='添加工资账套';
                this.activeIndex = 0;
                this.salary={
                    basicSalary: 0,
                    trafficSalary: 0,
                    lunchSalary: 0,
                    bonus: 0,
                    pensionPer: 0,
                    pensionBase: 0,
                    medicalPer: 0,
                    medicalBase: 0,
                    accumulationFundPer: 0,
                    accumulationFundBase: 0,
                    name: ''
                }
            }
        }
    }
</script>

<style scoped>

</style>